/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmiranda.afd;

import java.util.Iterator;
import java.util.Vector;
import sagex.api.*;

/**
 *
 * @author Tom Miranda
 */
public class AntiFavorite {
    
    private Object  airing;
    private Object  show;
    
    public AntiFavorite(Object Airing) {
        
        airing = Airing;
        
        if (airing == null) {
            Log.getInstance().write(Log.LOGLEVEL_WARN, "AntiFavorite: null Airing.");
            show = null;
            return;
        }
        
        show = getShow(airing);
        
        if (show == null) {
            Log.getInstance().write(Log.LOGLEVEL_ERROR, "AntiFavorite: null Show.");
            airing = null;
        }

    }
    
    public boolean isAntiFavorite() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        String data = ds.getAntiFavoriteStatus();     
        
        if (data == null) {
            return false;
        } else
            return data.equalsIgnoreCase("true");        
        
    }
    
    public boolean setAntiFavorite() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        if (ds.setAntiFavoriteStatus("true")) {
            markFutureAntiFavorite();
            return true;
        }
        
        Log.getInstance().write(Log.LOGLEVEL_WARN, "setAntiFavorite: Failed to set status.");
        return false;        
    }
    
    private void markFutureAntiFavorite() {
        
        long now = Utility.Time();
        
        String title = ShowAPI.GetShowTitle(airing);
        Vector allAirings = Database.SearchSelectedExactFields(title, false, true, false, false, false, false, false, false, false, false, "T");
        Log.getInstance().write(Log.LOGLEVEL_TRACE, "markFutureAntiFavorite: Found additional airings " + allAirings.size());
        
        for (Iterator it = allAirings.iterator(); it.hasNext();) {
            
            Object airing = it.next();
            
            if (AiringAPI.GetAiringStartTime(airing) > now) {
                Log.getInstance().write(Log.LOGLEVEL_TRACE, "markFutureAntiFavorite: Found future airing starting at " + Utility.PrintDateFull(AiringAPI.GetAiringStartTime(airing)) + " " + Utility.PrintTimeFull(AiringAPI.GetAiringStartTime(airing)));
                if (isMarkDontLike()) {
                    Log.getInstance().write(Log.LOGLEVEL_TRACE, "markFutureAntiFavorite: Setting it DontLike.");
                    AiringAPI.SetDontLike(airing);
                } else {
                    Log.getInstance().write(Log.LOGLEVEL_TRACE, "markFutureAntiFavorite: Not setting it DontLike.");    
                }
                
                if (isMarkWatched()) {
                    Log.getInstance().write(Log.LOGLEVEL_TRACE, "markFutureAntiFavorite: Setting it Watched.");
                    AiringAPI.SetWatched(airing);
                } else {
                    Log.getInstance().write(Log.LOGLEVEL_TRACE, "markFutureAntiFavorite: Not setting it Watched.");
                }
            }
        }
        
    }
    
    public boolean clearAntiFavorite() {
        if (airing == null)
            return false;
        else {
            DataStore ds = new DataStore(show);
            return ds.setAntiFavoriteStatus("");
        }
    }
    
    public boolean removeAntiFavorite() {
        if (airing == null)
            return false;
        else {
            DataStore ds = new DataStore(show);
            return ds.removeRecordForShow();
        }
    }
    
    public boolean isMarkWatched() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        String data = ds.getMarkWatchedStatus();     
        
        if (data == null) {
            return true;
        } else
            return data.equalsIgnoreCase("true");        
        
    }  
    
    public boolean setMarkWatched() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        if (ds.setMarkWatchedStatus("true")) {
            return true;
        }
        
        Log.getInstance().write(Log.LOGLEVEL_WARN, "setMarkWatched: Failed to set status.");
        return false;        
    }
    
    public boolean clearMarkWatched() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        if (ds.setMarkWatchedStatus("false")) {
            return true;
        }
        
        Log.getInstance().write(Log.LOGLEVEL_WARN, "setMarkWatched: Failed to set status.");
        return false;        
    }       
    
    public boolean isMarkDontLike() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        String data = ds.getMarkDontLikeStatus();     
        
        if (data == null) {
            return true;
        } else
            return data.equalsIgnoreCase("true");        
        
    }
    
    public boolean setMarkDontLike() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        if (ds.setMarkDontLikeStatus("true")) {
            return true;
        }
        
        Log.getInstance().write(Log.LOGLEVEL_WARN, "setMarkDontLike: Failed to set status.");
        return false;        
    }
    
    public boolean clearMarkDontLike() {
        if (airing == null)
            return false;
        
        DataStore ds = new DataStore(show);
        if (ds.setMarkDontLikeStatus("false")) {
            return true;
        }
        
        Log.getInstance().write(Log.LOGLEVEL_WARN, "setMarkDontLike: Failed to set status.");
        return false;        
    }     
    
    // Turn Airing or MediaFile into a Show if possible.
    private static Object getShow(Object o) {
        if (AiringAPI.IsAiringObject(o))
            return AiringAPI.GetShow(o);
        else if (ShowAPI.IsShowObject(o))
            return o;
        else if (MediaFileAPI.IsMediaFileObject(o))
            return AiringAPI.GetShow(o);
        else
            return o;
    }    
    
}
